package com.da.javatest.niuke.baodian;

import com.da.javatest.niuke.common.TreeNode;

/**
 * @author chenlida
 * @date 2020/10/7 14:18
 * https://www.nowcoder.com/practice/60231d6931d543d4aadcb67851b21e4a?tpId=8&&tqId=11016&rp=1&
 * ru=/ta/cracking-the-coding-interview&qru=/ta/cracking-the-coding-interview/question-ranking
 */
public class C23Successor {
    private int result = -1;
    private boolean active = false;

    public int findSucc(TreeNode root, int p) {
        inOrder(root, p);
        return result;
    }

    public void inOrder(TreeNode root, int target) {
        if (root == null) { return; }
        inOrder(root.left, target);
        if (active) {
            result = root.val;
            active = false;
            return;
        }
        if (root.val == target) {
            active = true;
        }
        inOrder(root.right, target);
    }
}
